package com.cicadasmall.system.wrapper;


import com.cicadasmall.common.constant.Constant;
import com.cicadasmall.common.func.Fn;
import com.cicadasmall.common.utils.SpringContextUtils;
import com.cicadasmall.common.utils.WarpsUtils;
import com.cicadasmall.common.wrapper.EntityWrapper;
import com.cicadasmall.data.domain.DictDO;
import com.cicadasmall.system.service.IDictService;
import com.cicadasmall.system.vo.DictVO;

import java.util.List;

public class DictWrapper implements EntityWrapper<DictDO, DictVO> {

    private final static IDictService dictService;

    static {
        dictService = SpringContextUtils.getBean(IDictService.class);
    }

    public static DictWrapper newBuilder() {
        return new DictWrapper();
    }

    @Override
    public DictVO entityVO(DictDO entity) {
        DictVO entityVo = WarpsUtils.copyTo(entity, DictVO.class);
        List<DictDO> childList = dictService.findByParentId(entity.getId());
        entityVo.setChildren(childList);
        if (Fn.isNotNull(entityVo.getParentId()) && Fn.notEqual(entityVo.getParentId(), Constant.PARENT_ID)) {
            DictDO parentDict = dictService.getById(entityVo.getParentId());
            entityVo.setParentName(parentDict.getDictName());
        }
        return entityVo;
    }


    @Override
    public List<DictVO> treeVO(List<DictDO> list) {
        return null;
    }
}